﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PMCSerialTransfer
{
    public class CommFactory
    {
        public byte[] Packet;

        public enum Command : byte
        {
            RecvData = 0,
            SendData = 1,
            RecvFile = 2,
            SendFile = 3,
            RecvMem = 4,
            SendMem = 5,
            RecvList = 6,
            SendList = 7,
            SndACK = 8,
            RecvACK = 9,
            Abort = 10,
            ReqData = 11,
            SndFileInfo = 12,
            SndFileName = 13,
            SndFileSize = 14,
            ReqSend = 15,
            RecvRAM = 16,
            SndRAM = 17,
            RecvMemInfo = 18,
            SendEEPROM = 19,
            RecvEEPROM = 20,
            Unmount = 21
        }

        public bool Factory(byte[] data)
        {
            APacket packet;
            bool pok = false;
            //int count = 0;

            switch (data[1])
            {
                case (byte)Command.SendData:
                    packet = new Packets.RecvDataPacket();
                    break;
                case (byte)Command.RecvFile:
                    packet = new Packets.RecvFileNamePacket();
                    break;
                case (byte)Command.RecvMemInfo:
                    packet = new Packets.RecvMemNamePacket();
                    break;
                case (byte)Command.SndACK:
                    packet = new Packets.SndACKPacket();
                    //packet.Command = (byte)Command.SndACK;
                    break;
                case (byte)Command.SendFile:
                    packet = new Packets.RecvFilePacket();
                    break;
                case (byte)Command.RecvData:
                    packet = new Packets.SndDataPacket();
                    break;
                case (byte)Command.RecvACK:
                    packet = new Packets.RecvAckPacket();
                    break;
                case (byte)Command.ReqData:
                    packet = new Packets.ReqDataPacket();
                    break;
                case (byte)Command.SendList:
                    packet = new Packets.RecvFileList();
                    break;
                case (byte)Command.SndFileSize:
                    packet = new Packets.RecvFileSizePacket();
                    break;
                case (byte)Command.ReqSend:
                    packet = new Packets.ReqSendPacket();
                    break;
                case (byte)Command.RecvList:
                    packet = new Packets.RecvFileListPacket();
                    break;
                default:
                    packet = new Packets.NullPacket();
                    break;

            }

            //while (!pok)
            //{
                pok = packet.Packet(data);
                //if (!pok)
                //{
                //    count++;
                //}
                pok = packet.EndProcess(pok);

                //if (count == 10)
                //    return false;
            //}
            Packet = Globals.DataPacket;
            return true;

        }
    }
}
